home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 May / CMCD0504.ISO / Software / Freeware / Programare / gdiplusdelphi / demos / Using Regions / Clipping with a Region / GDITEST48.dpr
Encoding:
Text File  |  2003-10-15  |  3.7 KB  |  137 lines

  1. program GDITEST48;
  2.  
  3. uses
  4.   Windows,
  5.   Messages,
  6.   SysUtils,
  7.   GDIPAPI,
  8.   GDIPOBJ;
  9.  
  10.  
  11. // One of the properties of the Graphics class is the clipping region.
  12. // All drawing done by a given Graphics object is restricted to the clipping
  13. // region of that Graphics object. You can set the clipping region by
  14. // calling the SetClip method.
  15.  
  16. // The following example constructs a path that consists of a single polygon.
  17. // Then the code constructs a region based on that path. The address of the
  18. // region is passed to the SetClip method of a Graphics object, and then two
  19. // strings are drawn.
  20.  
  21. Procedure OnPaint(DC: HDC);
  22. var
  23.   graphics : TGPGraphics;
  24.   path: TGPGraphicsPath;
  25.   region: TGPRegion;
  26.   Pen: TGPPen;
  27.   fontFamily: TGPFontFamily;
  28.   font: TGPFont;
  29.   solidBrush: TGPSolidBrush;
  30. const
  31.   polyPoints : array[0..3] of TGPPoint =
  32.    ((x: 10 ; y: 10 ),
  33.     (x: 150; y: 10 ),
  34.     (x: 100; y: 75 ),
  35.     (x: 100; y: 150));
  36. begin
  37.   graphics := TGPGraphics.Create(DC);
  38.   path:= TGPGraphicsPath.Create;
  39.   path.AddPolygon(PGPPoint(@polyPoints), 4);
  40.  
  41.   // Construct a region based on the path.
  42.   region := TGPRegion.Create(path);
  43.  
  44.   // Draw the outline of the region.
  45.   pen:= TGPPen.Create(MakeColor(255, 0, 0, 0));
  46.   graphics.DrawPath(pen, path);
  47.  
  48.   // Set the clipping region of the Graphics object.
  49.   graphics.SetClip(region);
  50.  
  51.   // Draw some clipped strings.
  52.   fontFamily:= TGPFontFamily.Create('Arial');
  53.   font:= TGPFont.Create(fontFamily, 36, FontStyleBold, UnitPixel);
  54.   solidBrush:= TGPSolidBrush.Create(MakeColor(255, 255, 0, 0));
  55.  
  56.   graphics.DrawString('A Clipping Region', 20, font,
  57.     MakePoint(15.0, 25.0), solidBrush);
  58.  
  59.   graphics.DrawString('A Clipping Region', 20, font,
  60.     MakePoint(15.0, 68.0), solidBrush);
  61.  
  62.   graphics.Free;
  63.   path.Free;
  64.   region.Free;
  65.   Pen.Free;
  66.   fontFamily.Free;
  67.   font.Free;
  68.   solidBrush.Free;
  69. end;
  70.  
  71.  
  72. function WndProc(Wnd : HWND; message : UINT; wParam : Integer; lParam: Integer) : Integer; stdcall;
  73. var
  74.   Handle: HDC;
  75.   ps: PAINTSTRUCT;
  76. begin
  77.   case message of
  78.     WM_PAINT:
  79.       begin
  80.         Handle := BeginPaint(Wnd, ps);
  81.         OnPaint(Handle);
  82.         EndPaint(Wnd, ps);
  83.         result := 0;
  84.       end;
  85.  
  86.     WM_DESTROY:
  87.       begin
  88.         PostQuitMessage(0);
  89.         result := 0;
  90.       end;
  91.  
  92.    else
  93.       result := DefWindowProc(Wnd, message, wParam, lParam);
  94.    end;
  95. end;
  96.  
  97. var
  98.   hWnd     : THandle;
  99.   Msg      : TMsg;
  100.   wndClass : TWndClass;
  101. begin
  102.    wndClass.style          := CS_HREDRAW or CS_VREDRAW;
  103.    wndClass.lpfnWndProc    := @WndProc;
  104.    wndClass.cbClsExtra     := 0;
  105.    wndClass.cbWndExtra     := 0;
  106.    wndClass.hInstance      := hInstance;
  107.    wndClass.hIcon          := LoadIcon(0, IDI_APPLICATION);
  108.    wndClass.hCursor        := LoadCursor(0, IDC_ARROW);
  109.    wndClass.hbrBackground  := HBRUSH(GetStockObject(WHITE_BRUSH));
  110.    wndClass.lpszMenuName   := nil;
  111.    wndClass.lpszClassName  := 'GettingStarted';
  112.  
  113.    RegisterClass(wndClass);
  114.  
  115.    hWnd := CreateWindow(
  116.       'GettingStarted',       // window class name
  117.       'Clipping with a Region',       // window caption
  118.       WS_OVERLAPPEDWINDOW,    // window style
  119.       Integer(CW_USEDEFAULT), // initial x position
  120.       Integer(CW_USEDEFAULT), // initial y position
  121.       Integer(CW_USEDEFAULT), // initial x size
  122.       Integer(CW_USEDEFAULT), // initial y size
  123.       0,                      // parent window handle
  124.       0,                      // window menu handle
  125.       hInstance,              // program instance handle
  126.       nil);                   // creation parameters
  127.  
  128.    ShowWindow(hWnd, SW_SHOW);
  129.    UpdateWindow(hWnd);
  130.  
  131.    while(GetMessage(msg, 0, 0, 0)) do
  132.    begin
  133.       TranslateMessage(msg);
  134.       DispatchMessage(msg);
  135.    end;
  136. end.
  137.